//
//  FFPayAlertView.m
//  FFMall
//
//  Created by IT.Bear on 16/8/22.
//  Copyright © 2016年 IT.Bear. All rights reserved.
//

#import "FFPayAlertView.h"
#import <Masonry.h>

@interface FFPayAlertView ()
@property (weak, nonatomic) IBOutlet UILabel *totalprice;
@property (weak, nonatomic) IBOutlet UIView *wepayView;
@property (weak, nonatomic) IBOutlet UIView *alipayView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *alipayHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *weipayHeight;

@end
@implementation FFPayAlertView

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.layer.cornerRadius = 10;
    self.clipsToBounds = YES;
    self.layer.masksToBounds = YES;
    self.layer.borderWidth = 0.5;
    self.layer.borderColor = RGB_CELL_GRAY.CGColor;
}

- (IBAction)payWithAlipay:(id)sender {
    if (self.payBlock) {
        self.payBlock(@"1");
    }
    [self cancel:nil];
}
- (IBAction)payWithWeChat:(id)sender {
    if (self.payBlock) {
        self.payBlock(@"2");
    }
    [self cancel:nil];
}


- (IBAction)cancel:(id)sender {
    
    if (self.payBlock) {
        self.payBlock(@"11");
    }
}

- (void)showWithTotalPrice:(FBOrderModel *)order withBlock:(ChoosePayBlock )block inView:(UIView *)view
{
    self.payBlock = block;
    [self showPayType:order.payMethods];
//    self.totalprice.text = [NSString stringWithFormat:@"实付：%@",[FFTools removeFloatAllZero:order.amount_payable]];
    [view addSubview:self];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(view.mas_top).offset(120);
        if (IPHONE_WIDTH<=375) {
            make.centerX.mas_equalTo(view.mas_centerX);
            make.width.mas_equalTo(250);
        }
        else{
            make.left.mas_equalTo(view.mas_left).offset(74);
            make.right.mas_equalTo(view.mas_right).offset(-74);
        }

        make.height.mas_equalTo(107 + order.payMethods.count*45);
    }];
    [self showInView:view];
}

- (void)showPayType:(NSArray *)payArr
{
    if (payArr.count == 1) {
        FBPayTypeModel *model = payArr[0];
        if ([model.payMethodId integerValue] == 1 || [model.payMethodId integerValue] == 4) {
            //只有支付宝
            self.hiddenWeipay = YES;
        }else{
            self.hiddenAlipay = YES;
        }
    }
    if (payArr.count == 0) {
        self.hiddenWeipay = YES;
        self.hiddenAlipay = YES;
    }
}

- (void)showInView:(UIView *)view
{
    CGPoint startPoint = CGPointMake(view.center.x, -FRAMEH(self));
    self.layer.position = startPoint;
    //damping:阻尼，范围0-1，阻尼越接近于0，弹性效果越明显
    //velocity:弹性复位的速度
    [UIView animateWithDuration:.8 delay:0 usingSpringWithDamping:.5 initialSpringVelocity:1.0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.layer.position=view.center;
        
    } completion:^(BOOL finished) {
        
    }];

}

- (void)setHiddenAlipay:(BOOL)hiddenAlipay
{
    if (hiddenAlipay) {
        _alipayHeight.constant = 0;
        [_alipayView setHidden:YES];
    }
}

- (void)setHiddenWeipay:(BOOL)hiddenWeipay
{
    if (hiddenWeipay) {
        _weipayHeight.constant = 0;
        [_wepayView setHidden:YES];
    }
}
@end
